from flask import Flask, Blueprint, render_template, session, redirect, url_for, request, Response, jsonify
# 这里注意要导入models中的models，而不是extends中的models
from App.models import *

dormblue = Blueprint('dormblue', __name__)
water_size = 200
elec_size = 200


@dormblue.route('/getDorm', methods=['GET', 'POST'])
def get_dorm():
    result_dict = []
    sql = 'select * from student,dorm where student.sid = dorm.master'
    result = db.session.execute(sql)
    for row in result:
        result_dict.append({
            'did': row[5],
            'studentNum': row[6],
            'waterfee': row[7],
            'electfee': row[8],
            'water': row[9],
            'electricity': row[10],
            'temperature': row[11],
            'humidity': row[12],
            'door': row[13],
            'monitor': row[14],
            'building': row[15],
            'masterSid': row[16],
            'masterName': row[1]
        })
    return jsonify(result_dict)


@dormblue.route('/getDormById', methods=['GET', 'POST'])
def get_dorm_byid():
    did = request.values.get('did')
    result_dict = []
    sql = 'select * from student,dorm where student.sid = dorm.master and dorm.did = ' + did
    result = db.session.execute(sql)
    for row in result:
        result_dict.append({
            'did': row[5],
            'studentNum': row[6],
            'waterfee': row[7],
            'electfee': row[8],
            'water': row[9],
            'electricity': row[10],
            'temperature': row[11],
            'humidity': row[12],
            'door': row[13],
            'monitor': row[14],
            'building': row[15],
            'masterSid': row[16],
            'masterName': row[1]
        })
    return jsonify(result_dict)


@dormblue.route('/WaterAndElect', methods=['GET', 'POST'])
def water_elect():
    did = request.values.get('did')
    result_dict = []
    sql = 'select * from student,dorm where student.sid = dorm.master and dorm.did = ' + did
    result = db.session.execute(sql)
    for row in result:
        result_dict.append({
            'waterfee': row[7],
            'electfee': row[8],
            'water': row[9],
            'electricity': row[10],
            'water_size': water_size,
            'elec_size': elec_size
        })
    return jsonify(result_dict)


# 测试
@dormblue.route('/water-elec')
def view_water_elec():
    return render_template("water-elec-moni.html")
